<?php
class UCenter {
	/**
	 * 默认获取字段
	 * @var string
	 */
	const COLUMNS = 'user_name,password,email,session_id,session_expire';
	
	/**
	 * 添加一个用户
	 * @param array $user
	 */
	public static function add_user($user){
		if(empty($user)){
			return self::result(false, '用户不能为空！');
		}
		if(!isset($user['user_name'])){
			return self::result(false, '用户名不能为空！');
		}
		if(strlen($user['user_name'])<5){
			return self::result(false, '用户名太短！');
		}
		if(!isset($user['site_id'])){
			return self::result(false, '未指定用户所属网站！');
		}
		if(!isset($user['password']) || strlen($user['password'])<6){
			return self::result(false, '用户密码长度太短！');
		}
		$user_mod = new UserModel();
		if(isset($user['email'])){
			if(!validate_email($user['email'])){
				return self::result(false, '邮件格式不正确！');
			}
			//验证邮箱是否存在
			if($user_mod->exists_attrs(array('site_id'=>SITE_ID, 'email'=>$user['email']))){
				return self::result(false, '此邮箱已被没注册！');
			}
		}
		if(isset($user['_id'])){
			//检查是否有重复用户
			if($user_mod->exists_key($user['_id'])){
				return self::result(false, '此ID的用户已经存在！');
			}
		}else{
			$user['_id'] = gen_guid();
		}
		$user['password'] = md5($user['password'] . FINGERPRINT);
		
		//检查是否存在
		if($user_mod->exists_attrs(array('site_id'=>$user['site_id'],'user_name'=>$user['user_name']))){
			return self::result(false, '此用户名已经存在！');
		}
		
		//保存
		$user_mod->insert($user);
		return self::result(true, '用户添加成功！', $user);
	}
	
	/**
	 * 更新用户信息，本方法仅检查用户名、密码、SiteID
	 * @param array $user
	 */
	public static function update_user_info($user){
		if(empty($user)){
			return self::result(false, '用户不能为空！');
		}
		if(!isset($user['_id'])){
			return self::result(false, '未指定要修改的用户！');
		}
		if(!isset($user['site_id'])){
			return self::result(false, '未指定用户所属网站！');
		}
		$user_mod = new UserModel();
		//检查是否有重复用户
		if(!$user_mod->exists_key($user['_id'])){
			return self::result(false, '此ID的用户已经不存在！');
		}
		if(isset($user['password'])){
			$password = $user['password'];
			//如果有指定密码
			if(utf8_strlen($password) === 32){
				//当成已经加密过的，表示不修改密码
				unset($user['password']);
			}elseif(empty($password)){
				//不修改密码
				unset($user['password']);
			}else{
				//当成要修改的，检查密码
				if(strlen($password)<6){
					return self::result(false, '用户密码长度太短！');
				}
				$user['password'] = md5($password . FINGERPRINT);
			}
		}
		
		//保存
		$user_mod->update_by_attrs(array('_id'=>$user['_id'], 'site_id'=>$user['site_id']), $user);
		return self::result(true, '用户修改成功！', $user);
	}
	
	/**
	 * 设置用户密码
	 * @param string $user_id
	 * @param string $password
	 */
	public static function set_password($user_id, $password){
		if(strlen($password)<5){
			return self::result(false, '密码太短！');
		}
		$user_mod = new UserModel();
		$user = $user_mod->get_by_key($user_id,'user_name');
		if(empty($user)){
			return self::result(false,'用户不存在！');
		}
		
		//加密
		$password = md5($password . FINGERPRINT);
		
		//修改密码
		$user_mod->update_by_key($user_id, array('password'=>$password));
		return self::result(true, '密码修改成功！');
	}
	
	//FINGERPRINT = Ds8GeY4;
	public static function login($site_id, $user_name, $password, $columns=null){
		if(empty($site_id)){
			return self::result(false, '网站不能为空！');
		}
		if(empty($user_name) || strlen($user_name)<4){
			return self::result(false, '用户名不正确！');
		}
		if(empty($password) || strlen($password)<6){
			return self::result(false, '密码输入不正确！');
		}
		if($columns === null){
			$columns = UCenter::COLUMNS;
		}
		//进行用户登录操作
		$user_mod = new UserModel();
		$user = $user_mod->get_by_attrs(array(
			'site_id'	=> $site_id,
			'user_name'	=> $user_name,
		), $columns);
		if(empty($user)){
			return self::result(false, '用户不存在！');
		}
		//判断密码
		$pwd = md5($password . FINGERPRINT);
		if($pwd != $user['password']){
			return self::result(false, '密码不正确！');
		}
		return self::result(true, '登录成功！', $user);
	}
	
	/**
	 * 获取用户信息
	 * @param string $site_id
	 * @param string $user_id
	 */
	public static function get_user_info($site_id, $user_id, $columns=null){
		if($columns === null){
			$columns = UCenter::COLUMNS;
		}
		$user_mod = new UserModel();
		return $user_mod->get_by_attrs(array('site_id'=>$site_id,  '_id'=>$user_id), $columns);
	}
	
	/**
	 * 利用ticket进行单点登录
	 */
	public static function auto_login($columns=null){
		if(isset($_REQUEST['ticket'])){
			$ticket = trim($_REQUEST['ticket']);
			if($columns === null){
				$columns = UCenter::COLUMNS;
			}
			//解密
			$ticket_str = decrypt($ticket);
			list($session_id, $site_id, $user_id) = explode(';', $ticket_str);
			//依赖于UCenter的验证系统，可以通过UCenter进行单点登录
			$user = UCenter::get_user_info($site_id, $user_id, $columns);
			if(!empty($user)){
				//验证Session是否过期
				if($user['session_id']!==$session_id || $user['session_expire']<time()){
					//已经过期
					return false;
				}else{
					//合法，登录成功
					return array(
						'user'			=> $user,
						'session_id' 	=> $session_id,
					);
				}
			}
		}
	}

	/**
	 * 更新用户状态，重新设置用户登录有效期，以防止过期
	 * @param string $user_id
	 */
	public static function update_user_state($user_id, $session_key, $session_expire){
		$user_mod = new UserModel();
		$user_mod->update_by_key($user_id, array('session_id'=>$session_key, 'session_expire'=>$session_expire));
	}
	
	public static function logout($user_id){
		$user_mod = new UserModel();
		$user_mod->delete_column_by_key($user_id, 'session_id,session_expire');
	}
	
	private static function result($success, $msg, $user=null){
		if(!$success){
			return array(
				'result'	=> false,
				'info'		=> $msg,
			);
		}else{
			return array(
				'result'	=> true,
				'info'		=> $msg,
				'data'		=> $user
			);
		}
	}
	
	public static function delete_user($user_id, $site_id){
		$user_mod = new UserModel();
		$condition = array('site_id'=>$site_id, '_id'=>$user_id);
		if(!$user_mod->exists_attrs($condition)){
			return self::result(false, '要删除的用户不存在或者已被删除！');
		}
		$user_mod->delete_by_attr($condition);
		return self::result(true, '删除成功！');
	}
}

?>